package com.hexinfo.dmpro.data.disaster.recovery.backups.model;

import cn.hutool.core.util.StrUtil;
import lombok.Data;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @Package: com.hexinfo.dmpro.data.disaster.recovery.backups.model
 * @ClassName DisasterRecoveryParam
 * @Description: 生成数据冷备流程请求参数
 * @Author: wz.li
 * @Date 2023/10/11 15:48
 * @Version v2.0.2
 **/
@Data
public class DisasterRecoveryParam {

    /**
     * 导出到HDFS的带端口号的目标Port
     **/
    private String hdfsUrls;

    /**
     * 导出到本地的目标目录
     **/
    private String localDir;

    /**
     * 导出源Hive的查询SQL
     **/
    private String hiveQuerySql;

    /**
     * 导出指定的库名
     **/
    private String databaseName;

    /**
     * 导出指定的表名
     **/
    private String tableName;

    /**
     * 导出到文件中设定的分隔符
     **/
    private String separator;

    /**
     * 导入方式(全量覆盖/增量插入)
     **/
    private String importType = "TOTAL";

    /**
     * 导入的指定分区名
     **/
    private String partitionName;

    /**
     * 导入的指定分区值
     **/
    private String partitionValue;

    /**
     * 分区字段配置
     **/
    private String partitionConfiguration;

    /**
     * 建表语句
     **/
    private String createTableSql;

    private String createTableSqlFilePath;

    private String remoteFilePath;

    private String remoteBackUpFilePath;

    public String getExportHdrsPath() {
        return StrUtil.format("{}/dataBackUps/export/{}/{}/{}", hdfsUrls, databaseName, tableName, getFileName());
    }

    public String getImportHdrsPath() {
        return StrUtil.format("{}/dataBackUps/import/{}/{}/{}", hdfsUrls, databaseName, tableName, getFileName());
    }

    public String getExportLocalDir() {
        return StrUtil.format("{}/export/{}/{}", localDir, databaseName, tableName);
    }

    public String getExportLocalPath() {
        return StrUtil.format("{}/export/{}/{}/{}", localDir, databaseName, tableName, getFileName());
    }

    public String getImportLocalPath() {
        return StrUtil.format("{}/import/{}/{}/{}", localDir, databaseName, tableName, getFileName());
    }

    public String getCreateTableDDLFilePath() {
        return StrUtil.format("{}/export/{}/{}/{}", localDir, databaseName, tableName, tableName + "_ddl.sql");
    }

    public String getBackupTarGzFileDir() {
        return StrUtil.format("{}/export/{}/{}", localDir, databaseName, tableName);
    }

    public String getBackupTarGzFilePath() {
        return StrUtil.format("{}/{}", getBackupTarGzFileDir(), getTarGzFileName());
    }

    public List<File> getBackupFiles() {
        List<File> files = new ArrayList<>();
        files.add(new File(getExportLocalPath()));
        files.add(new File(getCreateTableDDLFilePath()));
        return files;
    }

    public String getTableName() {
        return tableName;
    }

    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    public String getFileName() {
        if (StrUtil.isNotBlank(partitionValue)) {
            return tableName + "_" + partitionValue + ".txt";
        } else {
            return tableName + ".txt";
        }
    }

    public String getTarGzFileName() {
        if (StrUtil.isNotBlank(partitionValue)) {
            return tableName + "_" + partitionValue + ".tar.gz";
        } else {
            return tableName + ".tar.gz";
        }
    }
}
